package com.itheima.d07_net_udp_manytomany;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Objects;

/**
 * 目标：了解UDP网络编程中的服务端
 */
public class Server {

    public static void main(String[] args) throws Exception {
        //1.创建接收数据的服务端对象（接收韭菜的人）
        DatagramSocket client = new DatagramSocket(10086);

        //2.创建接收数据的数据包对象（装韭菜的盘子）
        byte[] buffer = new byte[1024*64]; //接收数据容器
        int sendMsgLength = buffer.length;   //发送数据的长度

        DatagramPacket packet = new DatagramPacket(buffer,sendMsgLength);

        //3.接收数据（接收韭菜）
        System.out.println("服务器端已启动... ...");
        while (true) {
            client.receive( packet );  //阻塞方法，一直等待接收客户端的数据

            String receiveMsg = new String(buffer,0,packet.getLength());

            System.out.println("数据已接收："+receiveMsg);

            if(Objects.equals(receiveMsg, "886")){
                break; //跳出循环
            }
        }

        //4.资源释放
        client.close();
    }
}
